/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import agent.AgentMBean;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServerConnection;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

/**
 *
 * @author alan
 */
public class ThreadMBeanRegister implements Runnable {

    private ObjectName controle;
    private MBeanServerConnection JMXSC;
    private String ipNMS;

    public ThreadMBeanRegister(ObjectName controle, MBeanServerConnection JMXSC, String ipNMS) {
        this.controle = controle;
        this.JMXSC = AgentMBean.makeAgent(ipNMS);
        this.ipNMS = ipNMS;
    }

    
    
    @Override
    public void run() {
        while (true) {
            try {
                if (controle.getKeyProperty("type").equals("MACHINEWINDOWS")) {
                    if (!JMXSC.isRegistered(controle)) {

                        JMXSC.createMBean("mbeans.AgentWindows", controle);
                        System.out.println("Registrada !!");
                    }
                } else {
                    if (controle.getKeyProperty("type").equals("MACHINELINUX")) {
                        if (!JMXSC.isRegistered(controle)) {
                            JMXSC.createMBean("mbeans.AgentLinux", controle);
                            System.out.println("Registrada!");
                        }
                    }
                }
                TimeUnit.SECONDS.sleep(2);
                this.JMXSC = AgentMBean.makeAgent(ipNMS);
            } catch (NullPointerException a) {
                try {
                    System.out.println("Error");
                    TimeUnit.SECONDS.sleep(5);
                    this.JMXSC = AgentMBean.makeAgent(ipNMS);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ReflectionException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstanceAlreadyExistsException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MBeanRegistrationException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MBeanException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NotCompliantMBeanException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadMBeanRegister.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

  
}
